﻿using UnityEngine;
using UnityEditor;
using System.Collections;

[CustomPropertyDrawer(typeof(PUEXDynamicColor))]
public class PUEXDynamicColorPropertyDrawer : PropertyDrawer {
	
	public override float GetPropertyHeight (SerializedProperty property, GUIContent label) 
	{
		return 18f * 2;
	}
	
	public override void OnGUI (Rect position, SerializedProperty property, GUIContent label) 
	{
		var colorTypeProperty = property.FindPropertyRelative ("type");
		var paramNameProperty = property.FindPropertyRelative ("paramName");
		var param2NameProperty = property.FindPropertyRelative ("param2Name");
		var valueProperty = property.FindPropertyRelative ("value");
		var value2Property = property.FindPropertyRelative ("value2");
		var gradientProperty = property.FindPropertyRelative ("gradient");
		
		label = EditorGUI.BeginProperty(position, label, property);
		Rect contentPosition = EditorGUI.PrefixLabel(position, label);
		
		contentPosition.height = 18f;
		var oldColorType = (PUEXDynamicColor.DynamicColorType)colorTypeProperty.enumValueIndex;
		EditorGUI.PropertyField(contentPosition, colorTypeProperty, GUIContent.none);
		var newColorType = (PUEXDynamicColor.DynamicColorType)colorTypeProperty.enumValueIndex;
		if (newColorType != oldColorType) {
			// reset unused property
			switch (newColorType) {
			case PUEXDynamicColor.DynamicColorType.DCT_FIXED:
				param2NameProperty.stringValue = "";
				value2Property.colorValue = Color.black;
				break;
			case PUEXDynamicColor.DynamicColorType.DCT_RANDOM:
				//NEEDFIX: reset gradientProperty
				break;
			case PUEXDynamicColor.DynamicColorType.DCT_GRADIENT:
				paramNameProperty.stringValue = "";
				valueProperty.colorValue = Color.white;
				param2NameProperty.stringValue = "";
				value2Property.colorValue = Color.black;
				//NEEDFIX: reset gradientProperty
				break;
			}
		}
		
//		EditorGUI.indentLevel = 1;
//		position = EditorGUI.IndentedRect(position);
		EditorGUIUtility.labelWidth = 7f * 5;
		contentPosition.width = position.width / 6; contentPosition.height = 18f;
		
		switch ((PUEXDynamicColor.DynamicColorType)colorTypeProperty.enumValueIndex) {
		case PUEXDynamicColor.DynamicColorType.DCT_FIXED:
			contentPosition.x = position.x + contentPosition.width * 4; contentPosition.y = position.y + 18f; 
			EditorGUI.PropertyField(contentPosition, valueProperty, GUIContent.none);
			contentPosition.x += contentPosition.width;
			EditorGUI.PropertyField(contentPosition, paramNameProperty, new GUIContent(" <--"));
			break;
		case PUEXDynamicColor.DynamicColorType.DCT_RANDOM:
			contentPosition.x = position.x + contentPosition.width * 2; contentPosition.y = position.y + 18f; 
			EditorGUI.PropertyField(contentPosition, valueProperty, GUIContent.none);
			contentPosition.x += contentPosition.width;
			EditorGUI.PropertyField(contentPosition, paramNameProperty, new GUIContent(" <--"));
			
			contentPosition.x += contentPosition.width;
			EditorGUI.PropertyField(contentPosition, value2Property, GUIContent.none);
			contentPosition.x += contentPosition.width;
			EditorGUI.PropertyField(contentPosition, param2NameProperty, new GUIContent(" <--"));
			break;
		case PUEXDynamicColor.DynamicColorType.DCT_GRADIENT:
			contentPosition.x = position.x + contentPosition.width * 2; contentPosition.y = position.y + 18f; contentPosition.width *= 4;
			EditorGUI.PropertyField(contentPosition, gradientProperty, GUIContent.none);
			break;
		}
		
		EditorGUI.EndProperty();
	}
	
}
